<template>
  <h1>登录页面</h1>
  <el-card style="width: 500px;margin: 0 auto;">
    <el-form label-width="100px">
      <el-form-item label="用户名"><el-input placeholder="请输入用户名" v-model="user.username"></el-input> </el-form-item>
      <el-form-item label="密码"><el-input type="password" placeholder="请输入密码" v-model="user.password"></el-input> </el-form-item>
      <el-form-item ><el-button @click="login()">登录</el-button> </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>

import {ref} from "vue";
import qs from 'qs';
import axios from 'axios';
import {ElMessage} from "element-plus";
import router from "@/router";


/*const user = ref({username:"",password:""});
const login = ()=>{
  let data = qs.stringify(user.value);
  axios.post('http://localhost:8081/v1/users/login',data)
      .then((response)=>{
        if (response.data.code==2001){
          //从JsonResult中得到UserVO
          let user = response.data.data;
          ElMessage.success("登录成功!欢迎"+user.nickname+"回来!");
          //把登录的用户信息保存到LocalStorage对象里面
          //LocalStorage里面只能保存字符串类型的数据
          //往里面存的如果是JS对象需要先将JS对象转成JSON格式的字符串
          localStorage.setItem('user',JSON.stringify(user));

          router.push('/');//跳转到首页
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}*/

const user=ref({username:"",password:""});
console.log("初始数据user对象"+user)
const login=()=>{
  console.log("触发")
  let data=qs.stringify(user.value);
  console.log("用户输入数据"+data)
  axios.post('http://localhost:8081/v1/users/login',data)
      .then(
          //箭头函数
          (response)=>{//##//单词拼错了
            console.log("响应数据"+qs.stringify(response.data))
            if (response.data.code==2001){
              // console.log("进入判断")
              let user=response.data.data;
              ElMessage.success("登录成功!欢迎"+user.nickname+"回家！");
              // console.log("user的数据"+user)
              localStorage.setItem('user',JSON.stringify(user));
              // console.log("浏览器存储")
              //跳转页面
              router.push('/');//跳转到首页
            }else{
              ElMessage.error(reponse.data.msg);
            }
          }
      )
}
</script>

<style scoped>

</style>